package com.wanlian.router.ui.me;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.smart.android.ui.BaseActivity;
import com.smart.android.utils.Utility;
import com.smart.android.widget.EditTextWithClear;
import com.wanlian.router.GlobalInfo;
import com.wanlian.router.R;
import com.wanlian.router.bean.User;
import com.wanlian.router.net.NetWorkingClient;
import com.wanlian.router.net.ResponseData;
import com.wanlian.router.net.internal.INetCallBack;

import java.util.regex.Pattern;

import butterknife.BindView;

/**
 * Created by Hyu on 2018/9/28.
 * Email: fvaryu@163.com
 */
public class EmailModifyActivity extends BaseActivity {

    @BindView(R.id.tv_email)
    EditTextWithClear mEditTextWithClear;


    @Override
    protected int layout() {
        return R.layout.activity_modify_email;
    }

    @Override
    public void initUI() {
        super.initUI();
        setTitle("更改邮箱");
        setNextText("完成");
        setNextColor(R.color.color_blue);
        setNextClickLisener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mEditTextWithClear.getText() == null ||
                        TextUtils.isEmpty(mEditTextWithClear.getText().toString())) {
                    showToast("请输入邮箱");
                    return;
                }
                String p = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
                boolean email =  Pattern.matches(p, mEditTextWithClear.getText().toString());
                if (!email) {
                    showToast("请输入有效邮箱");
                    return;
                }

               final User u = new User(GlobalInfo.getInstance().getUser());
                u.setEmail(mEditTextWithClear.getText().toString());
                NetWorkingClient.getInstance().modifyUser(getActivity(), u, new INetCallBack<User>() {
                    @Override
                    public void onFinish(ResponseData response, @Nullable User o) {
                        if (response.isSuccess()) {
                            showToast("修改成功");
                            GlobalInfo.getInstance().getUser().setEmail(u.getEmail());
                            finish();
                        }
                    }
                });
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        mEditTextWithClear.setText(GlobalInfo.getInstance().getUser().getEmail());
    }
}
